package com.zss.leetcode;

/**
 * 字符串最长的回文子串
 * @author fuguozhang
 * @create 2022/5/20
 */
public class LongestPalindromeString {

    public static void main(String[] args) {
        String s = "abbadce";
        String res = longestPalinromeString(s);
        System.out.println(res);
    }

    public static String longestPalinromeString(String s){
        if(s == null || s.length() < 2){
            return s;
        }
        int len = s.length();
        int maxLen = 1;
        int start = 0;
        char[] chars = s.toCharArray();
        for(int i=0; i<len-1; i++){
            for(int j=i+1; j<len; j++){
                if(j-i+1 > maxLen && isPalindrom(chars,i,j)){
                    maxLen = j-i+1;
                    start = i;
                }
            }
        }
        return s.substring(start,start+maxLen);
    }

    public static boolean isPalindrom(char[] chars,int i,int j){
        while (i<j){
            if(chars[i] != chars[j]){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
